/**
 * 抽奖
 */
// 获取转盘的奖项
var prizes = document.getElementsByClassName('lotto-item');
//当前的奖项
var now = 0;
var arr = [0,1,2,3,5,6,7,8];
function Lotto(start) {
	for( var j = 0; j < prizes.length; j++ ) {
		prizes[j].classList.remove('lotto-item-hover');
	}
	if( start > arr.length-1 ){
        start = 0;
        now = 0;
        prizes[arr[now]].classList.add('lotto-item-hover');
        now++;
	}else {
	    prizes[arr[start]].classList.add('lotto-item-hover');
	    now++;
	} 
}

/*
开始或者结束抽奖
 */
var lottoBtn = document.getElementById('start-lotto');
var lotto,lotto1;
//抽奖按钮是否可用,默认关闭
var isOk = true;
lottoBtn.addEventListener('click',function(){
	if( isOk ) {
		//抽奖过程中，抽奖按钮将不可用
	    isOk = !isOk;
	    this.children[1].classList.remove('tada');
		//开始自动抽奖,随机设置80-100之间的速度  [0] - 1   80-100
		var speed1 = Math.floor(Math.random()*21 + 80);
	    lotto = setInterval(function(){
	   		Lotto(now);
		},speed1);
		//3秒钟后清除第一个定时器，开启第二个定时器，速度降慢
		var speed2 = Math.floor(Math.random()*100 + 300);
		setTimeout(function(){
	        clearInterval(lotto);
	        lotto1 = setInterval(function(){
	   			Lotto(now);
			},speed2);
		},3000);
		//运行第二个定时器两秒后完成抽奖
		setTimeout(function() {
			//清除定时器
	        clearInterval(lotto1);
	        //抽奖开关可用
	        isOk = true;
            lottoBtn.children[1].classList.add('tada');
	        //跳转到中奖界面，传递参数now-1,代表中奖的奖品,同时当前的音乐播放进度
	        var prizeIndex = now - 1;
	        var music = document.getElementsByClassName('music')[0];
	        var tempo = music.currentTime;
	        setTimeout(function(){
	        	window.location.href = 'redeem.html?prizeIndex=' + prizeIndex + '&tempo=' + tempo;
	        },600);
	        
	    },5000);
	}
});


